﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FloodFree.Common;
using FloodFree.Web.Areas.Admin.ViewModels;
using System.Data;
using System.Data.Entity.Infrastructure;
using System.Data.SqlClient;

namespace FloodFree.Web.Areas.Admin.Controllers
{
    public class CitiesController : BaseController
    {
        public ActionResult Index()
        {
            if (TempData["ViewData"] != null)
            {
                ViewData = (ViewDataDictionary)TempData["ViewData"];
            }
            List<City> cities = db.Cities.Include("State").ToList();
            return View(cities);
        }

        [HttpGet]
        public ActionResult Create()
        {
            CityAddViewModel model = new CityAddViewModel();
            model.States = db.States.ToList<State>();
            model.City = new City();
            return View(model);
        }

        [HttpPost]
        public ActionResult Create(CityAddViewModel model)
        {
            if (ModelState.IsValid)
            {
                City city = model.City;
                city.State = db.States.Where(x => x.Id.Equals(model.City.State.Id)).First();
                db.Cities.Add(city);
                db.SaveChanges();
                return RedirectToAction("Index");
            }


            model.States = db.States.ToList<State>();
            return View(model);
        }

        [HttpGet]
        public ActionResult Edit(int id)
        {

            City city = (from s in db.Cities
                           where s.Id.Equals(id)
                           select s).First();

            if (city == null)
            {
                return HttpNotFound();
            }



            return View(city);
        }

        [HttpPost]
        public ActionResult Edit(City city)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    db.Entry(city).State = EntityState.Modified;
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
            }
            catch (DataException)
            {
                ModelState.AddModelError("", "Erro ao salvar os dados");
            }
            return View(city);
        }

        [HttpGet]
        public ActionResult Delete(int id)
        {
            City city = db.Cities
                .Where(x => x.Id == id)
                .First();

            if (city != null)
            {
                try
                {
                    db.Cities.Remove(city);
                    db.SaveChanges();
                    
                }
                catch (DbUpdateException e)
                {
                    SqlException s = e.InnerException.InnerException as SqlException;
                    if (s != null && s.Number == 547)
                    {
                        ModelState.AddModelError(string.Empty,
                            string.Format("Não é possível remover pois existem registros filhos cadastradas para esta cidade."));
                    }
                    else
                    {
                        ModelState.AddModelError("", "Erro ao remover o registro");
                    }
                }
                
            }
            TempData["ViewData"] = ViewData;
            return RedirectToAction("Index");

        }

        
    }
}
